package com.example.administrator.u17.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.u17.R;
import com.example.administrator.u17.adapter.QiangLiTuiJianMoreAdapter;
import com.example.administrator.u17.adapter.TwoPicMoreAdapter;
import com.example.administrator.u17.config.NetConfig;
import com.example.administrator.u17.model.QiangLiTuiJianMoreComics;
import com.example.administrator.u17.model.TwoPicMoreComics;
import com.example.administrator.u17.model.TwoPicMoreRootBean;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class TwoPicActivity extends AppCompatActivity {

    private PullToRefreshListView listView;
    private List<TwoPicMoreComics> list;
    private TwoPicMoreAdapter adapter;
    private TextView tvTitle;
    private ImageView ivBack;
    private String argValue;
    private String argName;
    private int pages=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_two_pic);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        listView.setMode(PullToRefreshBase.Mode.BOTH);
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadData(1);
                pages=1;
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadData(pages);
            }
        });
    }

    private void setData() {
        listView.setAdapter(adapter);
    }

    private void initData() {
        list=new ArrayList<>();
        adapter=new TwoPicMoreAdapter(this,list);
        loadData(pages);
    }

    private void initView() {
        listView=(PullToRefreshListView)findViewById(R.id.listview_activity_two_pic);
        tvTitle=(TextView)findViewById(R.id.tv_activity_two_pic_title);
        ivBack=(ImageView)findViewById(R.id.iv_activity_two_pic_back);
    }

    private void loadData(final int page){
        RequestParams params=new RequestParams(NetConfig.TWO_PIC_MORE_PATH+page);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                Gson g=new Gson();
                TwoPicMoreRootBean bean=g.fromJson(s,TwoPicMoreRootBean.class);
                if (pages==1){
                    list.clear();
                }
                pages++;
                list.addAll(bean.data.returnData.comics);
                adapter.notifyDataSetChanged();
                listView.onRefreshComplete();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
